package com.ynet.hmds28.user.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.alibaba.druid.util.StringUtils;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject;
import com.ynet.hm.user.service.PushService;
import com.ynet.hm.utils.UuidUtil;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hm.utils.enums.ExceptionEnum;
import com.ynet.hm.utils.exception.MyException;
import com.ynet.hmds28.user.dao.ServiceprovideDao;
import com.ynet.hmds28.user.model.Serviceprovide;
import com.ynet.hmds28.user.model.extend.FieldClass;
@Transactional
@HSFProvider(serviceInterface = PushService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class PushServiceImpl implements PushService {
	@Autowired
	private ServiceprovideDao serviceprovideDao;
//	@Value("${movieProvicde}")
//	public String movieProvicde;
	@Override
	public Result<?> pushService(String userId,String serviceName, String serviceUrl,
			String serviceMethod, String serviceCostRule, String flowControl,
			String importField, String exportField, String serivceDesc) {
		//开启GTS事务
		serviceprovideDao.enableTXC();
		if(StringUtils.isEmpty(serviceName)){
			throw new MyException(ExceptionEnum.SERVICENAME_IS_NULL);
		}
		if(StringUtils.isEmpty(serviceUrl)){
			throw new MyException(ExceptionEnum.SERVICEURL_IS_NULL);
		}
		if(StringUtils.isEmpty(serviceCostRule)){
			throw new MyException(ExceptionEnum.SERVICECOST_IS_NULL);
		}
		if(StringUtils.isEmpty(serviceMethod)){
			throw new MyException(ExceptionEnum.SERVICEMETHOD_IS_NULL);
		}
		if(StringUtils.isEmpty(flowControl)){
			throw new MyException(ExceptionEnum.SERVICEFLOWCONTROL_IS_NULL);
		}
		Serviceprovide sp=new Serviceprovide();
		sp.setCreatetime(new Date());
		StringBuilder sb1=new StringBuilder();
		if(!StringUtils.isEmpty(importField)){
			List<Object> stt=JSONObject.parseObject(importField,List.class);
			if(!CollectionUtils.isEmpty(stt)){
				for(int i=0;i<stt.size();i++){
					String str=stt.get(i).toString();
					FieldClass fc=JSONObject.parseObject(str, FieldClass.class);
					String importString=fc.getEnglishName()+"-"+fc.getChineseName();
					sb1.append(importString).append(",");
					
				}
			}
		}
		String strr=sb1.toString();
		sp.setImportfield(strr);
		StringBuilder sb2=new StringBuilder();
		if(!StringUtils.isEmpty(exportField)){
			List<Object> stt=JSONObject.parseObject(exportField,List.class);
			if(!CollectionUtils.isEmpty(stt)){
				for(int i=0;i<stt.size();i++){
					String str=stt.get(i).toString();
					FieldClass fc=JSONObject.parseObject(str, FieldClass.class);
					String exportString=fc.getEnglishName()+"-"+fc.getChineseName();
					sb2.append(exportString).append(",");
					
				}
			}
		}
		String exportStr=sb2.toString();
		String movieProvicde = UuidUtil.getUUid();
		sp.setExportfield(exportStr);
		sp.setServicecostrule(serviceCostRule);
		sp.setServicedesc(serivceDesc);
		sp.setServicemethod(serviceMethod);
		sp.setServicename(serviceName);
		sp.setServiceurl(serviceUrl);
		if(serviceprovideDao.selectByPrimaryKey("provicde001")!=null){
			if(serviceprovideDao.selectByPrimaryKey("provicde002")==null){
				movieProvicde = "provicde002";
			}
		}else{
			movieProvicde = "provicde001";
		}
		sp.setSeviceproviceid(movieProvicde);
		sp.setUserid(userId);
		serviceprovideDao.insertSelective(sp);
		return ResultUtil.success(movieProvicde);
	}

}
